#include <stdio.h>
#include <string.h>

#include "thread.h"
#include "ipc.h"

//#define USING_STATIC

#ifdef USING_STATIC
static mt_uint8_t pool[1024];
static mt_fifo_t fifo;
#endif
static mt_fifo_t *pfifo;


static void *thread1_func(void *arg)
{
    char buffer[3][32];
    mt_ssize_t lenght;
    mt_int32_t i;
    char *p = arg;
    
    printf("%s\r\n", p);
    while(1)
    {
        memset(buffer, 0, sizeof(buffer));
        lenght = mt_fifo_out(pfifo,
                          buffer,
                          3,
                          MT_WAITING_FOREVER);
        if(lenght > 0)
        {
            printf("lenght = %d\r\n", lenght);
            for(i=0; i<lenght; i++)
                printf("%s\r\n", buffer[i]);
        }
        else
            printf("take event failed\r\n");
    }
}

static void *thread2_func(void *arg)
{
    char *p = arg;
    char buffer[3][32] = {"thread2", "thread2_func", "thread2 func"};
    
    printf("%s\r\n", p);
    while(1)
    {
        mt_fifo_in_force(pfifo, buffer, 3);
        mt_delay(1000);
    }
}

static void *thread3_func(void *arg)
{
    char *p = arg;
    char buffer[1][32] = {"thread3"};
    
    printf("%s\r\n", p);
    while(1)
    {
        mt_fifo_in_force(pfifo, buffer, 1);
        mt_delay(2000);
    }
}

int fifo_test(void)
{
    mt_thread_t *thread1_handle;
    mt_thread_t *thread2_handle;
    mt_thread_t *thread3_handle;

#ifdef USING_STATIC
    mt_fifo_init(&fifo,
                 "test_fifo",
                 pool,
                 sizeof(pool),
                 32,
                 MT_IPC_FLAG_FIFO);
    pfifo = &fifo;
#else
    pfifo = mt_fifo_create("test_fifo",
                           5,
                           32,
                           MT_IPC_FLAG_FIFO);
    if(pfifo == MT_NULL)
    {
        printf("create fifo faied\r\n");
        return -1;
    }
#endif
    
    thread1_handle = mt_thread_create("thread1", 
                                      thread1_func, 
                                      "thread1", 
                                      1024, 
                                      12, 
                                      5);
    thread2_handle = mt_thread_create("thread2", 
                                      thread2_func, 
                                      "thread2", 
                                      1024, 
                                      11, 
                                      5);
    thread3_handle = mt_thread_create("thread3", 
                                      thread3_func, 
                                      "thread3", 
                                      1024, 
                                      10, 
                                      5);
    if((thread1_handle == MT_NULL) || (thread2_handle == MT_NULL) || (thread3_handle == MT_NULL))
    {
        printf("create thrtead faied\r\n");
        return -1;
    }
    mt_thread_start(thread1_handle);
    mt_thread_start(thread2_handle);
    mt_thread_start(thread3_handle);
    
    return 0;
}
